package com.neteast.androidclient.newscenter.domain;

import java.nio.ByteBuffer;
import java.util.Arrays;
import com.neteast.androidclient.newscenter.Global;

public class Logout {
	byte cmd=Global.NET_LOGOUT;
	/**通信协议*/
	public int protocolVersion; 
	/**报文id*/
	public int msgId;
	/**用户id*/
	public int userId;
	/**保留字段长度*/
	public int reserveLen;
	/**保留数据*/
	public byte[] reserve;
	
	/**
	 * @param protocolVersion int 通信协议
	 * @param msgId int 报文id
	 * @param userId int 用户id
	 */
	public Logout(int protocolVersion, int msgId, int userId) {
		super();
		this.protocolVersion = protocolVersion;
		this.msgId = msgId;
		this.userId = userId;
		this.reserveLen=0;
	}
	
	/**
	 * 
	 * @param protocolVersion int 通信协议
	 * @param msgId int 报文id
	 * @param userId int 用户id
	 * @param reserveLen 保留字段长度
	 * @param reserve 保留字段
	 */
	public Logout(int protocolVersion, int msgId, int userId, int reserveLen,byte[] reserve) {
		super();
		this.protocolVersion = protocolVersion;
		this.msgId = msgId;
		this.userId = userId;
		this.reserveLen = reserveLen;
		this.reserve = reserve;
	}


	public byte[] getMessage() {
		ByteBuffer byteBuffer=ByteBuffer.allocate(17+reserveLen);
		byteBuffer.put(cmd);
		byteBuffer.putInt(protocolVersion);
		byteBuffer.putInt(msgId);
		byteBuffer.putInt(userId);
		byteBuffer.putInt(reserveLen);
		if (reserveLen>0) {
			byteBuffer.put(reserve);
		}
		byteBuffer.flip();
		return byteBuffer.array();
	}

	@Override
	public String toString() {
		if (reserveLen>0) {
			return "Logout [cmd=" + cmd + ", protocolVersion=" + protocolVersion
					+ ", msgId=" + msgId + ", userId=" + userId + ", reserveLen="
					+ reserveLen + ", reserve=" + Arrays.toString(reserve) + "]";
		} else {
			return "Logout [cmd=" + cmd + ", protocolVersion=" + protocolVersion
					+ ", msgId=" + msgId + ", userId=" + userId + ", reserveLen="
					+ reserveLen +"]";
		}
	}
}
